/*******************************************************************************
 * Copyright (c) 2005, 2007 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package us.gaaoc.framework.dao;
/*
 * @author Frank
 */
import org.hibernate.Query;

import java.util.ArrayList;
import java.util.List;

import us.gaaoc.framework.model.DocPageRange;
import us.gaaoc.framework.model.DocumentPackage;
import us.gaaoc.framework.model.DocumentType;

public class DocPageRangeDAOHibernate extends
GenericHibernateDAO<DocPageRange, String, DocPageRangeDAO> implements
DocPageRangeDAO {
	
	protected DocPageRangeDAOHibernate() {}
	
	@SuppressWarnings("unchecked")
	public List<DocPageRange> findAllPageRangeByPackageUuid(DocumentPackage dp) {
		List<DocPageRange> results = new ArrayList<DocPageRange>();
		Query q = getSession().createQuery("from DocPageRange d where d.docPackage = ?");
		q.setEntity(0, dp);
		results = q.list();
		getSession().flush();
		return results; 
	}
	
	public DocumentType findDocTypeByRangePackageUuid(int start, int end, DocumentPackage dp) {
		Query q = getSession().createQuery("from DocPageRange d where d.pageRangeStart = ? and d.pageRangeEnd = ? and d.docPackage = ?");
		q.setInteger(0, start);
		q.setInteger(1, end);
		q.setEntity(2, dp);
		return (DocumentType)q.uniqueResult();
	}
	
	public DocumentType findDocTypeByUuid(String uuid) {
		Query q = getSession().createQuery("from DocPageRange d where d.uuid = ?");
		q.setString(0, uuid);
		return (DocumentType)q.uniqueResult();
	}
}